# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2022/8/14 16:39
# @Author : xyjiang1216
# @Email : jiangxuyan@nudt.edu.cn
# @File : ui_main.py
# @Version: OpenTSN_planner_builder 1.0
import sys
import time

from PySide6 import QtCore
from PySide6.QtCore import QSize, QPointF, QPoint
from PySide6.QtGui import QIcon, QMouseEvent
from PySide6.QtWidgets import QMainWindow, QApplication, QGraphicsScene, QPushButton

from UI.ui.test_ui import Ui_Form

from qt_material import apply_stylesheet
import qdarkstyle


class DragButton(QPushButton):
    def __init__(self, title, parent):
        super().__init__(title, parent)
        self.iniDragCor = [0, 0]
        self.i = 0
        # self.setMouseTracking(True)

    def mousePressEvent(self, e):

        if e.buttons() == QtCore.Qt.LeftButton:
            # self.mouseMoveEvent(e)
            pass
        # 右键按下
        elif e.buttons() == QtCore.Qt.RightButton:
            pass
        print("ppp", e.position())
        print(e.scenePosition())
        # self.iniDragCor[0] = e.scenePosition().x()
        # self.iniDragCor[1] = e.scenePosition().y()
        self.iniDragCor[0] = e.position().x()
        self.iniDragCor[1] = e.position().y()

    def mouseMoveEvent(self, e: QMouseEvent):
        if e.button() == QtCore.Qt.NoButton:
            pass
        # if e.button() == QtCore.Qt.LeftButton:
        print('1', e.buttons())
        print('2', e.button())
        if e.buttons() == QtCore.Qt.LeftButton:
            # print(e.flags())
            print(e.button(), '%d' % self.i)
            self.i += 1
            # if e.buttons() != QtCore.Qt.MouseButton.RightButton:
            #     print('right')
            #     return
            # if e.button() == QtCore.Qt.NoButton:
            x = e.position().x() - self.iniDragCor[0]
            # y = e.pos().y() - self.iniDragCor[1]
            y = e.position().y() - self.iniDragCor[1]

            cor = QPoint(x, y)
            self.move(self.mapToParent(cor))  # 需要maptoparent一下才可以的,否则只是相对位置。
            # self.move(cor)  # 需要maptoparent一下才可以的,否则只是相对位置。

            print('drag button event,', time.time(), e.position(), e.position().x(), e.position().y())


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)

        self.setWindowIcon(QIcon('../img/win_logo.png'))

        self.iniDragCor = [0, 0]

        # self.button = QPushButton('', self)
        self.button = DragButton('', self)
        self.button.setMaximumSize(30, 30)
        self.button.setMaximumSize(30, 30)
        icon = QIcon()
        icon.addFile('img/sw_mini.png')
        self.button.setIcon(icon)
        self.button.setIconSize(QSize(30, 30))

        # self.button.setMouseTracking(False)

        # self.button.setStyleSheet("QPushButton{background:rgba(0,0,0,0);border:1px solid rgba(0,0,0,0);}"
        #                      "QPushButton:hover{background-color: rgb(119,136,153);border:none;color:rgb(255, 255, "
        #                      "255);} "
        #                      "QPushButton:checked{background-color: rgb(119,136,153);border:none;color:rgb(255, 255, "
        #                      "255);}")

        # handle = QGraphicsRectItem(QRect(10, 10, 50, 50))
        # proxy = QGraphicsProxyWidget(handle)
        #
        # proxy.setWidget(button)
        # handle.setBrush(QtCore.Qt.blue)
        # self.graph_scene.addItem(handle)
        # proxy = self.graph_scene.addWidget(button)
        # proxy.setPos(QPointF(50, 50))

        # self.button.mousePressEvent = self.mousePressEvent_button
        # self.button.mouseMoveEvent = self.mouseMoveEvent_button

        # button = QPushButton('')
        # # button.resize(30, 30)
        # button.setMaximumSize(30, 30)
        # button.setMaximumSize(30, 30)
        # icon = QIcon()
        # icon.addFile('img/sw_mini.png')
        # button.setIcon(icon)
        # button.setIconSize(QSize(30, 30))
        # # button.setFlat(True)
        # # button.setStyleSheet("QPushButton{background:rgba(0,0,0,0);border:1px solid rgba(0,0,0,0);}")
        # button.setStyleSheet("QPushButton{background:rgba(0,0,0,0);border:1px solid rgba(0,0,0,0);}"
        #                      "QPushButton:hover{background-color: rgb(119,136,153);border:none;color:rgb(255, 255, "
        #                      "255);} "
        #                      "QPushButton:checked{background-color: rgb(119,136,153);border:none;color:rgb(255, 255, "
        #                      "255);}")
        # proxy = self.graph_scene.addWidget(button)
        # proxy.setPos(QPointF(100, 100))
        # self.iniDragCor = [100, 100]

        # self.ui.graphicsView.setScene(self.graph_scene)

    def mousePressEvent_button(self, e):
        # if e.buttons() == QtCore.Qt.LeftButton:
        print("ppp")
        print(type(self))
        self.iniDragCor[0] = e.position().x()
        self.iniDragCor[1] = e.position().y()

    def mouseMoveEvent_button(self, e):
        x = e.position().x() - self.iniDragCor[0]
        y = e.position().y() - self.iniDragCor[1]

        cor = QPoint(x, y)
        # self.move(self.mapToParent(cor))  # 需要maptoparent一下才可以的,否则只是相对位置。
        # self.move(cor)
        self.button.move(self.button.mapToParent(cor))

        print('drag button event,', e.position(), e.position().x(), e.position().y())


if __name__ == '__main__':
    app = QApplication([])
    window = MainWindow()

    # apply_stylesheet(app, theme='dark_teal.xml')
    # https://github.com/UN-GCPDS/qt-material/blob/master/README.md#usage
    app.setStyleSheet(qdarkstyle.load_stylesheet(qt_api='pyside6'))

    # window.setMouseTracking(False)
    window.show()
    sys.exit(app.exec())
